<?php

include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ModuleControllers.php';

class MyModules extends Modules {
    const table_member = 'mc_members';
    const table_member_permission = 'mc_member_permission';
    const table_product_category = 'mc_product_category';
    const table_specs = 'mc_specs';
    const table_product = 'mc_product';
    const table_product_specs = 'mc_product_specs';
    const table_news = 'mc_news';
    const table_news_category = 'mc_news_category';
    const table_product_maker = 'mc_product_maker';
    const table_product_maker_category = 'mc_product_maker_category';
    const table_product_order = 'mc_product_order';
    const table_product_order_detail = 'mc_product_order_detail';
    const table_slider = 'mc_slider';
    const table_customer_link = 'mc_mct_customer_link';
    const table_customer_contact = 'mc_customer_contact';

    //not change
    function __construct($aryConfig) {
        $this->setConfig($aryConfig);
    }

    public function getProductById($productId, &$aryData) {
        $sql = "SELECT  p.product_id,
                        p.product_title,
                        p.product_content,
                        p.product_title_filter,
                        p.product_category_id,
                        p.product_main_image,
                        p.product_small_image,
                        p.product_large_image,
                        p.product_highlight,
                        p.product_price_vnd,
                        p.product_is_vat,
                        p.product_store_name,
                        p.product_warranty_name,
                        p.product_promotional,
                        p.product_promotional_from,
                        p.product_promotional_to,
                        p.product_full_box,
                        p.product_transportation_name,
                        p.product_maker_id,
                        p.product_updated_date,
                        p.product_price_vnd_km,
                        p.product_price_active_km,
                        p.product_origin_name,
                        m.product_maker_name_filter,
                        m.product_maker_name,
                        m.product_maker_main_logo
                FROM " . self::table_product . " AS p
                LEFT JOIN " . self::table_product_maker . " AS m
                    ON  p.product_maker_id =  m.product_maker_id
                    WHERE product_del_flag = 0
                    AND product_id = $productId";
        $intOk = $this->getArray($sql, $aryData);
        return $intOk;
    }

    public function getProductPriceById($productId, &$aryData) {
        $sql = "SELECT product_price_vnd,
                        product_price_vnd_km,
                        product_price_active_km,
                        product_title,
                        product_main_image
                FROM " . self::table_product . "
                WHERE product_id = $productId
                AND product_del_flag = 0";
        $intOk = $this->getArray($sql, $aryData);
        return $intOk;
    }

    public function getProductSameMaker($makerId, $categoryId, $limit, &$aryProductSameMaker) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_main_image,
                        product_price_vnd,
                        product_small_image,
                        product_category_id
                    FROM " . self::table_product . "
                    WHERE product_del_flag = 0
                    AND product_maker_id = $makerId
                    AND product_category_id = $categoryId
                    ORDER BY product_updated_date DESC LIMIT $limit";
        $intOk = $this->getArray($sql, $aryProductSameMaker);
        return $intOk;
    }

    public function getProductSameCategory($categoryId, $limit, &$aryProductSameCategory) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_main_image,
                        product_price_vnd,
                        product_small_image,
                        product_category_id
                    FROM " . self::table_product . "
                    WHERE product_del_flag = 0
                    AND product_category_id = $categoryId
                    ORDER BY product_updated_date DESC LIMIT $limit";
        $intOk = $this->getArray($sql, $aryProductSameCategory);
        return $intOk;
    }

    public function getProductCategory(&$aryProductCategory) {
        $sql = "SELECT  category_id,
                        category_name,
                        category_parent,
                        category_level,
                        category_name_filter,
                        category_total_child
                    FROM " . self::table_product_category . "
                    WHERE category_del_flag = 0
                    ORDER BY category_order ASC";
        $intOk = $this->getArray($sql, $aryProductCategory);
        return $intOk;
    }

    public function getCategoryChildPath($categoryId) {

        $sql = "SELECT category_id
                    FROM " . self::table_product_category . "
                WHERE category_del_flag = 0 AND category_parent = $categoryId
                ORDER BY category_id DESC";

        $aryResultData = array();

        $intOk = $this->getArray($sql, $aryResultData);

        $aryCategoryId = array();

        foreach ($aryResultData as $key => $value) {

            $aryCategoryId[] = $value['category_id'];

            $aryCategoryId = array_merge($this->getCategoryChildPath($value['category_id']), $aryCategoryId);
        }

        return $aryCategoryId;
    }

    public function getNewsCategoryChildPath($newsCategoryId) {
        $sql = "SELECT news_category_id
                    FROM " . self::table_news_category . "
                WHERE news_category_del_flag = 0 AND news_category_parent = $newsCategoryId
                ORDER BY news_category_id DESC";

        $aryResultData = array();

        $intOk = $this->getArray($sql, $aryResultData);

        $aryCategoryId = array();

        foreach ($aryResultData as $key => $value) {

            $aryCategoryId[] = $value['news_category_id'];

            $aryCategoryId = array_merge($this->getNewsCategoryChildPath($value['news_category_id']), $aryCategoryId);
        }

        return $aryCategoryId;
    }

    public function getProductByListCatId($listCategoryId, $limit, &$aryProduct) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_category_id,
                        product_category_name,
                        product_maker_name,
                        product_store_name,
                        product_main_image,
                        product_highlight,
                        product_promotional,
                        product_price_vnd,
                        product_price_vnd_km,
                        product_price_active_km,
                        product_warranty_name,
                        product_transportation_name,
                        product_is_vat,
                        product_type_id
                FROM " . self::table_product . "
                    
                    WHERE product_del_flag = 0 ";

        $sql.="AND product_category_id IN($listCategoryId) AND product_price_vnd != 0";


        $sql.=" ORDER BY product_updated_date DESC, product_price_vnd DESC";

        $sql.=" LIMIT $limit";

        $intOk = $this->getArray($sql, $aryProduct);

        return $intOk;
    }

    public function getProductInNewsByCatId($listCategoryId, $limit, &$aryProduct) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_main_image
                FROM " . self::table_product . "
                    
                WHERE product_del_flag = 0 ";

        $sql.=" AND product_category_id IN($listCategoryId)";

        $sql.=" ORDER BY product_updated_date DESC";

        $sql.=" LIMIT $limit";

        $intOk = $this->getArray($sql, $aryProduct);

        return $intOk;
    }

    public function getProductByType($productType, &$aryProduct, $limit, $listCategory = null) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_category_id,
                        product_category_name,
                        product_maker_name,
                        product_store_name,
                        product_small_image,
                        product_main_image,
                        product_large_image,
                        product_highlight,
                        product_promotional,
                        product_price_vnd,
                        product_warranty_name,
                        product_transportation_name,
                        product_is_vat
                FROM " . self::table_product . "

                    WHERE product_del_flag = 0 ";

        $sql.="AND product_type_id = $productType";

        if (isset($listCategory)) {
            $sql.=" AND product_category_id IN($listCategory)";
        }


        $sql.=" ORDER BY product_updated_date DESC";

        $sql.=" LIMIT $limit";

        $intOk = $this->getArray($sql, $aryProduct);

        return $intOk;
    }

    public function getSubCatByCatId($categoryId, &$aryCategory) {
        $sql = "SELECT category_id,
                        category_name,
                        category_main_image,
                        category_name_filter,
                        category_total_child
                FROM " . self::table_product_category . "
                WHERE category_del_flag = 0
                AND category_parent = $categoryId
                ORDER BY category_order ASC";
        $intOk = $this->getArray($sql, $aryCategory);
        return $intOk;
    }

    public function getCategoryParentPath($categoryId) {
        $sql = "SELECT category_parent FROM " . self::table_product_category . "
                WHERE category_del_flag = 0 AND category_id = $categoryId";
        $aryResultData = array();
        $intOk = $this->getArray($sql, $aryResultData);
        $aryCategoryId = array();
        if ($aryResultData[0]['category_parent'] != 0) {
            $aryCategoryId[] = $aryResultData[0]['category_parent'];
            $aryCategoryId = array_merge($this->getCategoryParentPath($aryResultData[0]['category_parent']), $aryCategoryId);
        }
        return $aryCategoryId;
    }

    public function getNewsCategoryParentPath($newsCategoryId) {
        $sql = "SELECT news_category_parent FROM " . self::table_news_category . "
                WHERE news_category_del_flag = 0 AND news_category_id = $newsCategoryId";
        $aryResultData = array();
        $intOk = $this->getArray($sql, $aryResultData);
        $aryCategoryId = array();
        if ($aryResultData[0]['news_category_parent'] != 0) {
            $aryCategoryId[] = $aryResultData[0]['news_category_parent'];
            $aryCategoryId = array_merge($this->getNewsCategoryParentPath($aryResultData[0]['news_category_parent']), $aryCategoryId);
        }
        return $aryCategoryId;
    }

    public function getProduct($aryConditions, &$aryProduct, &$aryProductConditions) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_category_id,
                        product_category_name,
                        product_store_name,
                        product_main_image,
                        product_large_image,
                        product_highlight,
                        product_promotional,
                        product_price_vnd,
                        product_warranty_name,
                        product_transportation_name,
                        product_is_vat,
                        product_price_active_km,
                        product_price_vnd_km
                FROM " . self::table_product . "
                    WHERE product_del_flag = 0 ";

        $sql2 = "SELECT product_id,
                        product_maker_id,
                        product_price_vnd
                FROM " . self::table_product . "
                    WHERE product_del_flag = 0 ";

        $listCategoryId = $aryConditions['list_category'];

        $sql3 = "AND product_category_id IN($listCategoryId)";

        if ($aryConditions['product_maker_name'] != '' && $aryConditions['product_maker_name'] != 'all') {

            $sql3.="AND product_maker_name = '" . $aryConditions['product_maker_name'] . "'";
        }

        if (isset($aryConditions['product_price_from']) && is_numeric($aryConditions['product_price_from']) && isset($aryConditions['product_from_to']) && is_numeric($aryConditions['product_from_to'])) {
            $priceFrom = $aryConditions['product_price_from'];
            $priceTo = $aryConditions['product_from_to'];
            $sql3.=" AND product_price_vnd BETWEEN $priceFrom AND $priceTo";
        }

        if (isset($aryConditions['list_product_id'])) {

            $listProductId = $aryConditions['list_product_id'];

            $sql3.=" AND product_id IN($listProductId)";
        }

        if (isset($aryConditions['field']) && isset($aryConditions['order'])) {

            $field = $aryConditions['field'];

            $orderType = $aryConditions['order'];

            $sql3.=" ORDER BY $field $orderType";
        } else {

            $sql3.=" ORDER BY product_updated_date DESC";
        }

        $sql.=$sql3;

        $sql2.=$sql3;

        $aryProductConditions = array();

        if ($aryConditions['has_child_category'] == 0) {

            $this->getArray($sql2, $aryProductConditions);
        }

        $intOk = $this->paging($sql, $aryConditions['current_page'], $aryConditions['record_per_page'], $aryProduct);
        return $intOk;
    }

    public function getProductMakerByListId($listMakerId, &$aryProductMaker) {
        $sql = "SELECT product_maker_id,
                        product_maker_name,
                        product_maker_name_filter,
                        product_maker_main_logo
                FROM " . self::table_product_maker . "
                WHERE product_maker_del_flag = 0
                AND product_maker_id IN($listMakerId)";

        $intOk = $this->getArray($sql, $aryProductMaker);

        return $intOk;
    }

    public function getListProductMakerId($listSubCategoryId, &$aryProductMakerId){
        $sql = "SELECT product_maker_id
                FROM " . self::table_product. "
                WHERE product_del_flag = 0
                AND product_category_id IN($listSubCategoryId)";

        $intOk = $this->getArray($sql, $aryProductMakerId);

        return $intOk;
    }

    public function getSpecsByCategory($categoryId, &$aryProductSpecs) {
        $sql = "SELECT specs_id,
                        specs_parent,
                        specs_level,
                        specs_input_type,
                        specs_name,
                        specs_total_child
                FROM " . self::table_specs . "
                WHERE specs_category_id = $categoryId
                AND specs_del_flag = 0";
        $intOk = $this->getArray($sql, $aryProductSpecs);
        return $intOk;
    }

    public function getProductSpecsForFilterSpecs($categoryId, &$aryFilterSpecs) {
        $sql = "SELECT specs_id,
                        specs_name,
                        specs_parent,
                        specs_level,
                        specs_is_show
                FROM " . self::table_specs . "
                WHERE specs_del_flag = 0
                #AND specs_parent != 0
                AND specs_input_type != 3
                AND specs_input_type != 5
                AND specs_category_id = $categoryId";
        $intOk = $this->getArray($sql, $aryFilterSpecs);
        return $intOk;
    }

    public function getProductSpecsByProductId($listProductId, &$aryProductSpecs) {
        $sql = "SELECT product_specs_specs_id
                FROM " . self::table_product_specs . "
                WHERE product_specs_del_flag = 0
                AND product_specs_product_id IN($listProductId)";
        $intOk = $this->getArray($sql, $aryProductSpecs);
        return $intOk;
    }

    public function getSpecsDetailByProductId($productId, &$aryProductSpecs) {
        $sql = "SELECT product_specs_specs_id,
                        product_specs_specs_name
                FROM " . self::table_product_specs . "
                WHERE product_specs_del_flag = 0
                AND product_specs_product_id = $productId";
        $intOk = $this->getArray($sql, $aryProductSpecs);
        return $intOk;
    }

    public function getProductListIdBySpecsId($specsId, $countSpecs, &$aryProductListId) {
        $sql = "SELECT product_specs_product_id,
                        COUNT(product_specs_specs_id) AS ts_count
                    FROM " . self::table_product_specs . "
                    WHERE product_specs_del_flag = 0 AND product_specs_specs_id IN($specsId)
                    GROUP BY product_specs_product_id HAVING ts_count = $countSpecs";
        $intOk = $this->getArray($sql, $aryProductListId);
        return $intOk;
    }

    public function getSecsParentPath($specsId) {
        $sql = "SELECT specs_parent FROM " . self::table_specs . "
                WHERE specs_del_flag = 0 AND specs_id = $specsId";
        $aryResultData = array();
        $intOk = $this->getArray($sql, $aryResultData);
        $arySpecsId = array();
        if ($aryResultData[0]['specs_parent'] != 0) {
            $arySpecsId[] = $aryResultData[0]['specs_parent'];
            $arySpecsId = array_merge($this->getSecsParentPath($aryResultData[0]['specs_parent']), $arySpecsId);
        }
        return $arySpecsId;
    }

    public function getCategoryByListId($listCatParent, &$aryCategoryParent) {
        $sql = "SELECT category_id,
                        category_name,
                        category_name_filter,
                        category_keyword
                FROM " . self::table_product_category . "
                WHERE category_del_flag = 0
                AND category_id IN($listCatParent)";
        $intOk = $this->getArray($sql, $aryCategoryParent);
        return $intOk;
    }

    public function checkExistOrder($productId, $cookieShopCat, &$aryData) {
        $sql = "SELECT product_order_product_id
                FROM " . self::table_product_order . "
                WHERE product_order_product_id = $productId
                AND product_order_cookie_id = '" . $cookieShopCat . "'
                AND product_order_del_flag = 0";
        $intOk = $this->getArray($sql, $aryData);
        return $intOk;
    }

    public function checkExistOrderDetail($cookieShopCat, &$aryData) {
        $sql = "SELECT order_detail_id FROM " . self::table_product_order_detail . "
                        WHERE order_detail_cookie_id = '" . $cookieShopCat . "'
                        AND order_detail_del_flag = 0";
        $intOk = $this->getArray($sql, $aryData);
        return $intOk;
    }

    public function insertProductOrder($aryOrder) {
        $intOk = $this->insert(self::table_product_order, $aryOrder);
        return $intOk;
    }

    public function getProductOrderByCookie($cookieShopCat, &$aryProductOrder) {
        $sql = "SELECT product_order_id,
                        product_order_product_id,
                        product_order_product_name,
                        product_order_total_product,
                        product_order_product_price,
                        product_order_total_price,
                        product_order_active,
                        product_order_product_image,
                        product_order_category_id,
                        product_order_category_name,
                        product_order_category_filter
                FROM " . self::table_product_order . "
                WHERE product_order_cookie_id = '" . $cookieShopCat . "'
                AND product_order_del_flag = 0";
        $intOk = $this->getArray($sql, $aryProductOrder);
        return $intOk;
    }

    public function getProductIdInOrder($cookieShopCat, &$aryProductOrder) {
        $sql = "SELECT product_order_product_id
                FROM " . self::table_product_order . "
                WHERE product_order_cookie_id = '" . $cookieShopCat . "'
                AND product_order_del_flag = 0";
        $intOk = $this->getArray($sql, $aryProductOrder);
        return $intOk;
    }

    public function getProductOrderDetail($cookieShopCat, &$aryProductOrderDetail) {
        $sql = "SELECT order_detail_full_name,
                        order_detail_address,
                        order_detail_phone_number,
                        order_detail_email,
                        order_detail_address_delivery,
                        order_detail_method_delivery,
                        order_detail_delivery_name,
                        order_detail_method_payment,
                        order_detail_payment_name,
                        order_detail_active,
                        order_detail_company_name,
                        order_detail_company_address,
                        order_detail_company_tax_code
                FROM " . self::table_product_order_detail . "
                WHERE order_detail_del_flag = 0 AND order_detail_cookie_id = '" . $cookieShopCat . "' ";
        $intOk = $this->getArray($sql, $aryProductOrderDetail);
        return $intOk;
    }

    public function updateTotalItem($aryOrderUpdate, $aryConfig) {
        $intOk = $this->update(self::table_product_order, $aryOrderUpdate, $aryConfig);
        return $intOk;
    }

    public function insertOrderDetail($aryOrderDetail, &$returnOrderId) {
        $intOk = $this->insert(self::table_product_order_detail, $aryOrderDetail);
        $returnOrderId = $this->getLastInsertedID();
        return $intOk;
    }

    public function updateOrder($returnOrderId, $cookieShopCat) {

        $sql = "UPDATE " . self::table_product_order . " SET product_order_detail_id = $returnOrderId
                WHERE product_order_cookie_id = '" . $cookieShopCat . "'";
        $intOk = $this->Excute($sql);
        return $intOk;
    }

    public function updateProductOrderDetail($aryOrderDetail, $aryConfig) {
        $intOk = $this->update(self::table_product_order_detail, $aryOrderDetail, $aryConfig);
        return $intOk;
    }

    public function activeOderDetail($cookieShopCat) {
        $sql = "UPDATE " . self::table_product_order_detail . " SET order_detail_active = 1
                WHERE order_detail_cookie_id = '" . $cookieShopCat . "'";
        $intOk = $this->Excute($sql);
        return $intOk;
    }

    public function getSlider(&$aryData) {
        $sql = "SELECT slider_id,
                        slider_title,
                        slider_link,
                        slider_image
                FROM " . self::table_slider . "
                WHERE slider_del_flag = 0 ";
        $intOk = $this->getArray($sql, $aryData);
        return $intOk;
    }

    public function getTopNews(&$aryTopNews, $type, $limit) {
        $sql = "SELECT news_id,
                        news_title,
                        news_title_filter,
                        news_content_filter,
                        news_main_image,
                        news_large_image
                FROM " . self::table_news . "
                    WHERE news_del_flag = 0
                    AND news_type = $type
                ORDER BY news_updated_date DESC
                LIMIT $limit";
        $intOk = $this->getArray($sql, $aryTopNews);
        return $intOk;
    }

    public function getNewsLimit($limit, &$aryNews) {
        $sql = "SELECT news_id,
                        news_title,
                        news_title_filter,
                        news_content_filter,
                        news_main_image,
                        news_large_image
                FROM " . self::table_news . "
                    WHERE news_del_flag = 0
                ORDER BY news_updated_date DESC
                LIMIT $limit";
        $intOk = $this->getArray($sql, $aryNews);
        return $intOk;
    }

    public function getNewsCategory(&$aryCategory) {
        $sql = "SELECT  news_category_id,
                        news_category_name,
                        news_category_parent,
                        news_category_level,
                        news_category_filter,
                        news_category_product_category,
                        news_category_product_child_cat
                    FROM " . self::table_news_category . "
                    WHERE news_category_del_flag = 0 AND news_category_is_show = 1";

        $sql.=" ORDER BY news_category_order ASC";

        $intOk = $this->getArray($sql, $aryCategory);
        return $intOk;
    }

    public function getNewsCategoryByListId($listCategoryId, &$aryNewsCategory) {
        $sql = "SELECT  news_category_id,
                        news_category_name,
                        news_category_filter
                    FROM " . self::table_news_category . "
                    WHERE news_category_del_flag = 0 AND news_category_id IN($listCategoryId)";

        $intOk = $this->getArray($sql, $aryNewsCategory);
        return $intOk;
    }

    public function getNewsByCategoryId($catId, $limit, &$aryNewsData) {
        $sql = "SELECT news_id,
                        news_title,
                        news_title_filter,
                        news_content_filter,
                        news_main_image,
                        news_large_image
                FROM " . self::table_news . "
                    WHERE news_del_flag = 0
                    AND news_new_category_id = $catId
                ORDER BY news_updated_date DESC
                LIMIT $limit";
        $intOk = $this->getArray($sql, $aryNewsData);
        return $intOk;
    }

    public function getNewByListNewsCategory($strCatId, $limit, &$aryNewsData) {
        $sql = "SELECT news_id,
                        news_title_filter,
                        news_title,
                        news_main_image,
                        news_large_image,
                        news_content_filter,
                        news_updated_date
                FROM " . self::table_news . "
                WHERE news_del_flag = 0
                AND (news_new_category_id = $strCatId OR news_new_sub_category_id = $strCatId)";

        $sql.=" ORDER BY news_updated_date DESC
                LIMIT $limit";

        $intOk = $this->getArray($sql, $aryNewsData);
        return $intOk;
    }

    public function getSameNewByNewsCategory($strCatId, $limit, &$aryNewsData, $newId) {
        $sql = "SELECT news_id,
                        news_title_filter,
                        news_title,
                        news_updated_date
                FROM " . self::table_news . "
                WHERE news_del_flag = 0
                AND (news_new_category_id = $strCatId OR news_new_sub_category_id = $strCatId)";

        $sql.=" AND news_id != $newId";

        $sql.=" ORDER BY news_updated_date DESC
                LIMIT $limit";

        $intOk = $this->getArray($sql, $aryNewsData);
        return $intOk;
    }

    public function getTopProduct($type, $limit, $listProductCategory, &$aryProduct) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_main_image,
                        product_large_image
                FROM " . self::table_product . "
                WHERE product_del_flag = 0
                AND product_type_id = $type
                AND product_category_id IN($listProductCategory)
                ORDER BY product_updated_date DESC
                LIMIT $limit";
        $intOk = $this->getArray($sql, $aryProduct);
        return $intOk;
    }

    public function getProductCategoryInNews($newsCategoryId, &$aryData) {
        $sql = "SELECT news_category_product_child_cat FROM " . self::table_news_category . "
                WHERE news_category_del_flag = 0 AND news_category_id = $newsCategoryId";
        $intOk = $this->getArray($sql, $aryData);
        return $intOk;
    }

    public function getNewsDetail($newsId, &$aryNewsData) {
        $sql = "SELECT news_title,
                        news_content,
                        news_updated_date,
                        news_new_sub_category_id
                FROM " . self::table_news . "
                WHERE news_del_flag = 0
                AND news_id = $newsId
                LIMIT 1";

        $intOk = $this->getArray($sql, $aryNewsData);
        return $intOk;
    }

    public function updateNewsViews($newsId) {
        $sql = "UPDATE " . self::table_news . "
                SET news_total_views = news_total_views + 1
                WHERE news_id = $newsId";
        $intOk = $this->Excute($sql);
        return $intOk;
    }

    public function getNewsTopViews($limit, &$aryNewsTopViews) {
        $sql = "SELECT news_id,
                        news_title_filter,
                        news_title,
                        news_updated_date
                FROM " . self::table_news . "
                WHERE news_del_flag = 0";

        $sql.=" ORDER BY news_total_views DESC
                LIMIT $limit";

        $intOk = $this->getArray($sql, $aryNewsTopViews);
        return $intOk;
    }

    public function getProCatByNewCat($newsSubCategoryId, &$aryProCat) {
        $sql = "SELECT news_category_product_child_cat 
                    FROM " . self::table_news_category . "
                WHERE news_category_del_flag = 0
                AND news_category_id = $newsSubCategoryId";
        $intOk = $this->getArray($sql, $aryProCat);
        return $intOk;
    }

    public function searchProductData($aryConditions, &$aryProduct) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_category_id,
                        product_category_name,
                        product_store_name,
                        product_main_image,
                        product_highlight,
                        product_promotional,
                        product_price_vnd,
                        product_warranty_name,
                        product_transportation_name,
                        product_is_vat,
                        product_price_active_km,
                        product_price_vnd_km
                FROM " . self::table_product . "
                    WHERE product_del_flag = 0 ";

        if (isset($aryConditions['product_category_id'])) {

            $listCategoryId = $aryConditions['product_category_id'];

            $sql.=" AND product_category_id IN($listCategoryId)";
        }

        if (isset($aryConditions['product_maker_id']) && $aryConditions['product_maker_id'] != 0) {

            $sql.=" AND product_maker_id = '" . $aryConditions['product_maker_id'] . "' ";
        }

        if (strlen($aryConditions['product_keyword']) > 0) {

            $sql.=" AND product_title_search LIKE '%" . $aryConditions['product_keyword'] . "%'";
        }

        if ($aryConditions['product_price_to'] > 0) {
            $priceFrom = $aryConditions['product_price_from'];
            $priceTo = $aryConditions['product_price_to'];
            $sql.=" AND product_price_vnd BETWEEN $priceFrom AND $priceTo";
        }

        if(isset ($aryConditions['field']) && isset ($aryConditions['order'])){
            $orderBy = $aryConditions['order'];
            $field = $aryConditions['field'];
            $sql.=" ORDER BY $field $orderBy";
        }

        

        $intOk = $this->paging($sql, $aryConditions['current_page'], $aryConditions['record_per_page'], $aryProduct);
        return $intOk;
    }

    public function searchCategoryInProduct($aryConditions, &$aryCategoryProduct) {
        $sql = "SELECT product_category_id,
                        product_category_name
                FROM " . self::table_product . "
                WHERE product_del_flag = 0";
        $sql.=" AND product_title_search LIKE '%" . $aryConditions['keyword'] . "%' ";

        $intOk = $this->getArray($sql, $aryCategoryProduct);

        return $intOk;
    }

    public function searchAllProduct($aryConditions, &$aryProductSearch) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_main_image,
                        product_highlight,
                        product_price_vnd
                        
                FROM " . self::table_product . "
                    WHERE product_del_flag = 0 ";

        $sql.=" AND product_title_search LIKE '%" . $aryConditions['keyword'] . "%'";

        if (isset($aryConditions['product_category_id']) && is_numeric($aryConditions['product_category_id'])) {

            $sql.=" AND product_category_id = '" . $aryConditions['product_category_id'] . "' ";
        }

        $sql.=" ORDER BY product_price_vnd DESC";

        $intOk = $this->paging($sql, $aryConditions['current_page'], $aryConditions['record_per_page'], $aryProductSearch);
        return $intOk;
    }

    public function searchAllNews($aryConditions, &$arySearchNews) {
        $sql = "SELECT news_id,
                        news_title_filter,
                        news_title,
                        news_content_filter,
                        news_main_image
                FROM " . self::table_news . "
                WHERE news_del_flag = 0
                AND news_title_search LIKE '%" . $aryConditions['keyword'] . "%'";

        $intOk = $this->paging($sql, $aryConditions['current_page'], $aryConditions['record_per_page'], $arySearchNews);
        return $intOk;
    }

    public function countTotalNews($aryConditions, &$arySearchNews) {
        $sql = "SELECT news_id
                FROM " . self::table_news . "
                WHERE news_del_flag = 0
                AND news_title_search LIKE '%" . $aryConditions['keyword'] . "%'";

        $intOk = $this->getArray($sql, $arySearchNews);
    }

    public function getProductInfoByType($aryConditions, &$aryProuctInfo) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_category_id,
                        product_category_name,
                        product_store_name,
                        product_main_image,
                        product_highlight,
                        product_promotional,
                        product_price_vnd,
                        product_warranty_name,
                        product_transportation_name,
                        product_is_vat,
                        product_price_active_km,
                        product_price_vnd_km
                FROM " . self::table_product . "
                    WHERE product_del_flag = 0 ";

        if (isset($aryConditions['product_category_id']) && $aryConditions['product_category_id'] != 'all') {

            $listCategory = $aryConditions['product_category_id'];

            $sql.=" AND product_category_id IN($listCategory)";
        }

        if (isset($aryConditions['product_type_id']) && is_numeric($aryConditions['product_type_id'])) {

            $productType = $aryConditions['product_type_id'];

            $sql.=" AND product_type_id = $productType";
        }

        if (isset($aryConditions['field']) && isset($aryConditions['order'])) {
            $field = $aryConditions['field'];

            $orderType = $aryConditions['order'];

            $sql.=" ORDER BY $field $orderType";
        }

        $this->paging($sql, $aryConditions['current_page'], $aryConditions['record_per_page'], $aryProuctInfo);

        return $intOk;
    }

    public function deleteAllShopCat($aryConditions) {
        $sql = "UPDATE " . self::table_product_order . "
                SET product_order_del_flag = 1
                WHERE product_order_cookie_id = '" . $aryConditions['product_order_cookie_id'] . "'";
        $intOk = $this->Excute($sql);
        return $intOk;
    }

    public function deleteOrderById($aryDelete, $aryConfig) {
        $intOk = $this->update(self::table_product_order, $aryDelete, $aryConfig);
        return $intOk;
    }

    public function saveCustomerLink($aryCustomerLink) {
        $intOk = $this->insert(self::table_customer_link, $aryCustomerLink);
        return $intOk;
    }

    public function getProductByListId($listProductId, &$aryProduct) {
        $sql = "SELECT product_id,
                        product_title,
                        product_title_filter,
                        product_category_id,
                        product_content,
                        product_category_name,
                        product_maker_name,
                        product_store_name,
                        product_main_image,
                        product_highlight,
                        product_promotional,
                        product_price_vnd,
                        product_price_vnd_km,
                        product_price_active_km,
                        product_warranty_name,
                        product_transportation_name,
                        product_is_vat,
                        product_type_id
                FROM " . self::table_product . "
                WHERE product_del_flag = 0
                AND product_id IN($listProductId)";
        $intOk = $this->getArray($sql, $aryProduct);
        return $intOk;
    }
    
    public function saveCustomerContact($aryCustomer) {
        $intOk = $this->insert(self::table_customer_contact, $aryCustomer);
        return $intOk;
    }

}

?>
